import {
  examineList,
  examine,
  examineCount,
  getYidunHistory,
  queryExtendInfo
} from '@api/pattern/index.js'
import { postAction } from '@/api/manage'
import { pattern } from '@api/pattern/index'


const riskList = [{
  title: '原创／著作问题',
  value: "10",
},
{
  title: '社会和谐',
  value: "20",
},
{
  title: '政治敏感',
  value: "30",
},
{
  title: '文化歧视',
  value: "40",
},
{
  title: '语言问题',
  value: "50",
},
{
  title: '未成年问题',
  value: "60",
},
]
export const listMixin = {
  data() {
    return {
      riskList,
      risks: [],
      text: '',
      patternCode: '',
      order: [0, 10, 20, 40],
      current: 1,
      current1: 1,
      showY: false,
      current2: 1,
      current3: 1,
      current4: 1,
      spinning: false,
      examineRemark: '',
      showExamineRemark: '',
      riskFlag: '',
      size: 1,
      total: '',
      dataSource: [],
      showNext: true,
      groupName: '',
      detailList: [],
      patternImg: '',
      showButton: true,
      showSubmit: false,
      showWait: false,
      colorList: [],
      colorLength: '',
      designerUserName: '',
      fabricCode: '',
      whetherHasEnglish: '',
      form: this.$form.createForm(this),
      elementList: [],
      historyList: [],
      patternName: '',
      remarks: '',
      yidunvisible: false,
      visible: false,
      passType: '',
      showAll: false,
      showHis: false,
      operation: '',
      patternId: '',
      patternCodeSearch: '',
      meaning: '',
      defaultImg: 'https://img.bzhz.jnbygroup.com/cloth.png',
      showSkc: true,
      imgVisible: false,
      ifTag: 0,
      errortip1: '',
      errortip: '',
      errortip2: '',
      imgElementVisible: false,
      elementImageUrl: '',
      bandList: [],
      bandLeft: [],
      bandRight: [],
      patternList: [],
      key: '0',
      prevImageUrl: []
    }
  },

  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    },
    brand() {
      return this.$route.query.brand || ''
    },
    season() {
      return this.$route.query.season || ''
    },
  },
  created() {
    this.$nextTick(() => {
      // 禁用右键
      document.oncontextmenu = new Function("event.returnValue=false");
    });
  },
  mounted() {
    this.getCount()
    this.getAuth()
  },
  methods: {
    prevElementImg(url) {
      if (typeof url == 'string' && url != '') {
        this.elementImageUrl = url
        this.imgElementVisible = true
      }
    },

    prevImg(url, urlback, index) {
      this.prevImageUrl = []
      if (typeof url == 'string' && url != '') {
        this.prevImageUrl.push(url)
        this.prevImageUrl.push(urlback)
        this.imgVisible = true
      }
      this.$nextTick(() => {
        this.$refs['varousel2'].goTo(index)
      })
    },
    showBigPhoto(index) {
      this.prevImageUrl = []
      this.colorList.map((item) => {
        this.prevImageUrl.push(item.images[0].url)
      })
      this.imgVisible = true
      this.$nextTick(() => {
        this.$refs['varousel2'].goTo(index)
      })
    },
    bigImg(index) {
      this.prevImageUrl = []
      this.colorList.map((item) => {
        this.prevImageUrl.push(item.images[0].url)
      })
      this.imgList = this.prevImageUrl
      this.goToIndex = index
      setTimeout(() => {
        this.visibleImgs = true
      }, 100)
    },
    showModal(type) {
      this.passType = type
      this.visible = true
    },

    showYidun() {
      this.showY = true
    },
    closeYidun() {
      this.showY = false
    },
    showSubmitInfo() {
      this.showSubmit = true
    },
    getAuth() {
      try {
        postAction(`/imagemanage-center/account/list`, { jobNumber: this.jobNumber }).then(data => {
          if (data.code == 200) {
            if (data.data.records[0].tag.includes('法务')) {
              this.showWait = true
            } else {
              this.showWait = false

            }
          } else {
            this.$message.warning(data.msg)
          }
          this.loading = false
        })
      } catch (error) {
        this.loading = false
      }
      finally {
        this.loading = false

      }
    },
    closeSubmit() {
      this.showSubmit = false
    },
    showHistory(key) {
      this.showHis = true
      if (key == 1) {
        this.ifTag = 1
      }

    },
    closeHistory() {
      this.showHis = false
      this.ifTag = 0
    },
    handleSubmit(e) {
      e.preventDefault()
      this.current1 == 1
      this.current2 == 1
      this.current3 == 1
      this.current4 == 1
      this.getCount(1)
    },
    resetForm(e) {
      e.preventDefault()
      this.patternName = ''
      this.patternCodeSearch = ''
      this.text = ''
      this.getCount()
    },
    handleCancel(e) {
      this.visible = false
      this.examineRemark = ''
    },
    last() {
      if (this.key == 0) {
        this.current1 -= 1
      } else if (this.key == 10) {
        this.current2 -= 1
      } else if (this.key == 20) {
        this.current3 -= 1
      } else {
        this.current4 -= 1
      }
      this.getList(this.key, 7)
      this.$refs['varousel'].goTo(0)
    },
    next() {
      if (this.key == 0) {
        this.current1 += 1
      } else if (this.key == 10) {
        this.current2 += 1
      } else if (this.key == 20) {
        this.current3 += 1
      } else {
        this.current4 += 1
      }
      this.getList(this.key, 9)
      this.$refs['varousel'].goTo(0)
    },
    async getCount(key) {
      if (key) {
        this.count1 = 0
        this.count2 = 0
        this.count3 = 0
        this.count4 = 0
        this.current1 = 1
        this.current2 = 1
        this.current3 = 1
        this.current4 = 1
      }
      const data = {
        jobNumber: this.jobNumber,
        brand: this.brand,
        season: this.season,
        patternName: this.patternName,
        patternCode: this.patternCodeSearch,
        relationSkcFlag: 1,
        groupNameList: ['设计组', '外采组'],
      }
      try {
        const res = await examineCount('', data)
        if (res.code == 200) {
          res.data.map(item => {
            if (item.examineStatus == 0) {
              this.count1 = item.count
            } else if (item.examineStatus == 10) {
              this.count2 = item.count
            } else if (item.examineStatus == 20) {
              this.count3 = item.count
            } else if (item.examineStatus == 40) {
              this.count4 = item.count
            }
          })
          this.getList(this.key, 8)
        } else {
          this.$message.warning(res.msg)
        }
      } catch (error) {

      }
    },
    async getList(key, type) {
      this.spinning = true;
      this.key = key ? key : 0
      this.risks = []
      const data = {
        size: this.size,
        current: this.key == 0 ? this.current1 : this.key == 10 ? this.current2 : this.key == 20 ? this.current3 : this.current4,
        brand: this.brand,
        season: this.season,
        fabricCode: this.fabricCode,
        designerUserName: this.designerUserName,
        patternName: this.patternName,
        patternCode: this.patternCodeSearch,
        examineStatus: this.key,
        jobNumber: this.jobNumber,
        relationSkcFlag: 1,
        groupNameList: ['设计组', '外采组'],
      }
      try {
        const res = await examineList('', data)
        if (res.code == 200) {
          this.current = res.data.current
          if (!res.data.records[0]) {
            if (type == 8) {
              this.showAll = false
              if (res.data.current == 1 && this.key == 0) {
                this.count1 = 0
                this.current1 = 1
              } else if (res.data.current == 1 && this.key == 10) {
                this.count2 = 0
                this.current2 = 1
              } else if (res.data.current == 1 && this.key == 20) {
                this.count3 = 0
                this.current3 = 1
              } else if (res.data.current == 1 && this.key == 40) {
                this.count4 = 0
                this.current4 = 1
              }
              if (this.key == 0) {
                this.current1 -= 1
              } else if (this.key == 10) {
                this.current2 -= 1
              } else if (this.key == 20) {
                this.current3 -= 1
              } else if (this.key == 40) {
                this.current4 -= 1
              }
              if (this.current != 1) {
                this.getList(this.key)
                this.current -= 1
              } else {
                this.patternId = ''
                this.$message.warning('没有更多了')
              }
            } else {
              this.patternId = ''
              this.$message.warning('没有更多了')
              this.current -= 1
              if (this.key == 0) {
                this.current1 -= 1
              } else if (this.key == 10) {
                this.current2 -= 1
              } else if (this.key == 20) {
                this.current3 -= 1
              } else {
                this.current4 -= 1
              }
            }
            // this.last()
          } else {
            this.bandLeft = [];
            this.bandRight = []
            this.showAll = true
            this.id = res.data.records[0].id
            if (res.data.records[0].brandCode === '速写') {
              this.code = 'CROQUIS'
            } else if (res.data.records[0].brandCode === 'A PERSONAL NOTE 73') {
              this.code = 'APN 73'
            } else if (res.data.records[0].brandCode === 'jnby by JNBY') {
              this.code = '童装'
            } else if (res.data.records[0].brandCode === 'Pomme de terre') {
              this.code = '蓬马'
            } else if (res.data.records[0].brandCode === 'jnby for mini') {
              this.code = '婴童'
            } else {
              this.code = res.data.records[0].brandCode
            }
            this.total = res.data.total
            this.patternId = res.data.records[0].id
            this.groupName = res.data.records[0].groupName
            this.patternCode = res.data.records[0].code
            this.patternList = res.data.records[0]
            this.whetherHasEnglish = res.data.records[0].whetherHasEnglish == 1 ? '是' : '否'
            // this.riskFlag = res.data.records[0].riskFlag == 1 ? '是' : '否'
            this.colorList = res.data.records[0].colors
            this.colorLength = res.data.records[0].colors.length
            this.patternImg = res.data.records[0].colors[0] ? res.data.records[0].colors[0].images[0].url : ''
            this.showExamineRemark = res.data.records[0].examineRemark
            this.patternList.risk.map((item) => {
              this.riskList.map((riskitem) => {
                if (item == riskitem.value) {
                  this.risks.push(riskitem)
                }
              })
            })
            this.getElementList()
            this.getSkcList()
            this.getHistory()
            // this.getHistory1()
            this.getExtendDetail()
            if(this.showWait){
              this.getTag()
            }
            // this.getSensitiveword()

          }
          this.$refs['varousel'].goTo(0)
        } else {
          if (type == 9) {
            if (this.key == 0) {
              this.current1 -= 1
            } else if (this.key == 10) {
              this.current2 -= 1
            } else if (this.key == 20) {
              this.current3 -= 1
            } else {
              this.current4 -= 1

            }
          }
          if (type == 7) {
            if (this.key == 0) {
              this.current1 += 1
            } else if (this.key == 10) {
              this.current2 += 1
            } else if (this.key == 20) {
              this.current3 += 1
            } else {
              this.current4 += 1
            }
          }
          this.$message.warning(res.msg)
        }
      } catch (error) {
        if (type == 9) {
          if (this.key == 0) {
            this.current1 -= 1
          } else if (this.key == 10) {
            this.current2 -= 1
          } else if (this.key == 20) {
            this.current3 -= 1
          } else {
            this.current4 -= 1
          }
        }
        if (type == 7) {
          if (this.key == 0) {
            this.current1 += 1
          } else if (this.key == 10) {
            this.current2 += 1
          } else if (this.key == 20) {
            this.current3 += 1
          } else {
            this.current4 += 1
          }
        }
      } finally {
        this.spinning = false;
      }

    },
    getElementList() {
      const data = {
        patternId: this.patternId
      }
      postAction(`${pattern}element/list/by/pattern`, data).then(res => {
        if (res.code == 200) {
          this.elementList = res.data
          // console.log(this.elementList);
        }
      })
    },
    getSensitiveword() {
      const data = {
        contents: this.patternList.meaning
      }
      postAction(`${pattern}sensitiveword/recognize`, data).then(res => {
        if (res.code == 200) {
          if (res.data.length) {
            res.data.map((item) => {
              let regStr = "" + `(${item})`;
              let reg = new RegExp(regStr, "g");
              this.patternList.meaning = this.patternList.meaning.replace(reg, '<span style="color:red;">' + item + "</span>");
            })
          }
        }
      })
    },
    getSensitiveword1() {
      const data = {
        contents: this.detailList.supplyInspiration
      }
      postAction(`${pattern}sensitiveword/recognize`, data).then(res => {
        if (res.code == 200) {
          if (res.data.length) {
            res.data.map((item) => {
              let regStr = "" + `(${item})`;
              let reg = new RegExp(regStr, "g");
              this.detailList.supplyInspiration = this.detailList.supplyInspiration.replace(reg, '<span style="color:red;">' + item + "</span>");
            })
          }
        }
      })
    },
    getSensitiveword2() {
      const data = {
        contents: this.detailList.patternInspiration
      }
      postAction(`${pattern}sensitiveword/recognize`, data).then(res => {
        if (res.code == 200) {
          if (res.data.length) {
            res.data.map((item) => {
              let regStr = "" + `(${item})`;
              let reg = new RegExp(regStr, "g");
              this.detailList.patternInspiration = this.detailList.patternInspiration.replace(reg, '<span style="color:red;">' + item + "</span>");
            })
          }
        }
      })
    },
    getSensitiveword3() {
      const data = {
        contents: this.detailList.typefaceMeaning
      }
      postAction(`${pattern}sensitiveword/recognize`, data).then(res => {
        if (res.code == 200) {
          if (res.data.length) {
            res.data.map((item) => {
              let regStr = "" + `(${item})`;
              let reg = new RegExp(regStr, "g");
              this.detailList.typefaceMeaning = this.detailList.typefaceMeaning.replace(reg, '<span style="color:red;">' + item + "</span>");
            })
          }
        }
      })
    },
    async showyidunHistory(record) {
      this.yidunvisible = true
      const data = {
        dataId: record.dataId
      }
      const res = await getYidunHistory('log/query', data)
      if (res.code == 200) {
        this.historyList = res.data
      } else {
        this.$message.error(res.msg || res.message)
      }
    },
    resetHistory() {
      this.yidunvisible = false
    },
    async getExtendDetail() {
      const res = await queryExtendInfo('pattern', { patternId: this.patternId })
      if (res.code == 200) {
        this.detailList = res.data
        // console.log(this.detailList);
        if (this.detailList.moreFile || this.detailList.moreFile == '') {
          this.detailList.moreFile = this.detailList.moreFile.split(',')
          this.detailList.cooperationContractUrl = this.detailList.cooperationContractUrl.split(',')
          this.detailList.patternOriginUrl = this.detailList.patternOriginUrl.split(',')
          this.detailList.patternAuthorizationUrl = this.detailList.patternAuthorizationUrl.split(',')
          this.detailList.typefaceOriginUrl = this.detailList.typefaceOriginUrl.split(',')
          this.detailList.typefaceOriginProveUrl = this.detailList.typefaceOriginProveUrl.split(',')
          this.getSensitiveword1()
          this.getSensitiveword2()
          this.getSensitiveword3()
        }
      } else {
        this.$message.error('查找图案扩展信息失败')
      }
    },
    getHistory() {
      const data = {
        patternId: this.patternId,
        tag: '法务'
      }
      if (this.patternId) {
        postAction(`${pattern}pattern/examine/record`, data).then(res => {
          if (res.code == 200) {
            this.dataSource = res.data
            if (this.dataSource.length) {
              const newArr = []
              const newArr1= []

              this.dataSource.map((item) => {
                if(item.type == 20){
                  newArr.push(item.examineStatus)
                }else if(item.type == 10){
                  newArr1.push(item.examineStatus)
                }
              })
              if (newArr.includes('1')) {
                this.errortip = '易盾嫌疑'
              } else {
                this.errortip = ''
              }
              if (newArr1[0]=='20'){
                this.errortip1 = '法务不通过'
              } else if (newArr1[0]=='40') {
                this.errortip1 = '法务待定'
              } else {
                this.errortip1 = ''
              }
            }
          } else {
            this.$message.error(res.msg || res.message)
          }
        })
      } else {
        this.$message.error('无数据！')
      }

    },
    // getHistory1() {
    //   const data = {
    //     patternId: this.patternId,
    //     tag: '法务'
    //   }
    //   if (this.patternId) {
    //     postAction(`${pattern}pattern/examine/tag`, data).then(res => {
    //       if (res.code == 200) {
    //         if (res.data.length) {
    //           const newArr = []
    //           res.data.map((item) => {
    //             newArr.push(item.examineStatus)
    //           })
    //           if (newArr.includes('20')) {
    //             this.errortip1 = '法务不通过'
    //           } else if (newArr.includes('40')) {
    //             this.errortip1 = '法务待定'
    //           } else {
    //             this.errortip1 = ''
    //           }
    //         }
    //       } else {
    //         this.$message.error(res.msg || res.message)
    //       }
    //     })
    //   } else {
    //     this.$message.error('无数据！')
    //   }

    // },
    getTag() {
      const data = {
        patternId: this.patternId,
        jobNumber: this.jobNumber,
        tag: '审核小组'
      }
      postAction(`${pattern}pattern/examine/tag`, data).then(res => {
        if (res.code == 200) {
          if (res.data.length) {
            const newArr = []
            res.data.map((item) => {
              newArr.push(item.examineStatus)
            })
            if (newArr.includes('20')) {
              this.errortip2 = '审核小组有不通过'
            } else {
              this.errortip2 = ''
            }
          }
        } else {
          this.$message.error(res.msg || res.message)
        }
      })


    },
    getSkcList() {
      const data = {
        patternId: this.patternId
      }
      postAction(`${pattern}pattern/relationSkc`, data).then(res => {
        if (res.code == 200) {
          this.bandList = res.data
          this.bandList.map((item, index) => {
            if (index % 2 == 0) {
              this.bandLeft.push(item)
            } else {
              this.bandRight.push(item)
            }
          })
          if (!res.data) {
            this.showSkc = false
          } else {
            this.showSkc = true
          }
        }

      })
    },
    async pass() {
      if (this.passType == '20' && this.examineRemark == '') {
        this.$message.warning('请输入审核意见')
      } else {
        const data = {
          id: this.id,
          jobNumber: this.jobNumber,
          examineRemark: this.examineRemark,
          operation: this.passType,
        }
        try {
          this.spinning = true
          const res = await examine('', data)
          if (res.code == 200) {
            this.$message.success('操作成功,审核下一个')
            this.handleCancel()
            this.getCount()
          } else {
            this.$message.warning(res.msg || res.message)
          }
        } catch (error) {
        } finally {
          this.spinning = false;
        }

      }
    },
  },
}